package com.qifan.alb2b.service.resource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.qifan.alb2b.dao.CityInfoDao;
import com.qifan.alb2b.dao.GuideInfoDao;
import com.qifan.alb2b.domain.GuideInfoDomain;
import com.qifan.alb2b.domain.TravelItemInfoDomain;
import com.qifan.alb2b.exception.BusinessException;
import com.qifan.alb2b.service.route.SevenStPrice;
import com.qifan.alb2b.service.route.SixStPrice;
import com.qifan.alb2b.service.vo.Resource;
import com.qifan.alb2b.service.vo.ResourceVo;
import com.qifan.alb2b.service.vo.RouteDetailVo;
import com.qifan.alb2b.service.vo.RouteVo;
import com.qifan.alb2b.service.vo.StPriceTable;
import com.qifan.alb2b.util.Arith;
import com.qifan.alb2b.util.Constants;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.ExchangeUtil;
import com.qifan.alb2b.util.Page;
import com.qifan.alb2b.util.Stringer;

/**
 * 导游资源
 * @author hc24
 *
 */
@Component
public class GuideResource implements IResource {
	private static String[] header=new String[]{"ID","语种","时间段","价格/人"};
	
	private GuideInfoDao guideInfoDao;
	private CityInfoDao cityInfoDao;
	
	public void setGuideInfoDao(GuideInfoDao guideInfoDao) {
		this.guideInfoDao = guideInfoDao;
	}

	public void setCityInfoDao(CityInfoDao cityInfoDao) {
		this.cityInfoDao = cityInfoDao;
	}

	public void addOrUpdate(RouteVo routeVo, String routeDetailId,
			String[] resIds) {
//		if(routeVo.getGroupType()==Constants.GROUP_TYPE_SEVEN&&(resIds==null||resIds.length==0))
//			throw new BusinessException("标配资源必须选择");
		if(routeVo.getGroupType()==Constants.GROUP_TYPE_SEVEN&&resIds==null)
			throw new BusinessException("标配资源必须选择");
		//=====查找线路详情=====
		List<RouteDetailVo> detailList= routeVo.getDetailVoList();
		RouteDetailVo routeDetailVo = null;
		for(RouteDetailVo rdv:detailList){
			if(rdv.getRouteDetail().getDetailId().equals(routeDetailId)){
				routeDetailVo=rdv;
				break;
			}
		}
		
		if(routeDetailVo==null)
			return;
		//=====更新导游资源resList=====
		String countryId=routeVo.getRoute().getCountryId();
		GuideInfoDomain oldGuide=null;
		GuideInfoDomain newGuide=null;
		if(resIds!=null&&resIds.length>0){//代表1-6人团中要删除导游信息
			newGuide=guideInfoDao.getGuideById(resIds[0]);
		}else{
			newGuide=new GuideInfoDomain();
			newGuide.setGuideId("");
			newGuide.setPrice(0);
		}
		
		List<ResourceVo> resList=routeDetailVo.getResList();
		for(int i=0;i<resList.size();i++){
			ResourceVo rv=resList.get(i);
			if(rv.getResType()==Constants.RES_TYPE_GUIDE){
				oldGuide=(GuideInfoDomain) rv.getResource();
				if(resIds==null||resIds.length==0){
					resList.remove(i);//移除
					break;
				}
				if(!rv.getResId().equals(newGuide.getGuideId())){
					oldGuide=(GuideInfoDomain) rv.getResource();
					rv.setResId(resIds[0]);
					newGuide.setPrice((float) ExchangeUtil.changeMoney(newGuide.getPrice(),countryId));//在这里就把价格转化成人民币!!!
					rv.setResource(newGuide);
					break;
				}//end if
			}//end if
		}//end for
		
//		if(oldGuide==null&&routeVo.getGroupType()==Constants.GROUP_TYPE_SEVEN)
//			return;
		if(oldGuide==null){
			oldGuide=new GuideInfoDomain();
			oldGuide.setPrice(0);
			if(resIds!=null&&resIds.length!=0){//对于1-6人团不是标配资源，则增加导游信息
				ResourceVo rv=new ResourceVo();
				rv.setIsStd(1);
				rv.setResId(resIds[0]);
				newGuide.setPrice((float) ExchangeUtil.changeMoney(newGuide.getPrice(),countryId));//在这里就把价格转化成人民币!!!
				rv.setResource(newGuide);
				rv.setResType(Constants.RES_TYPE_GUIDE);
				resList.add(rv);
			}
		}
		
		//=====更新标配价格表StTable=====
		StPriceTable spt=routeVo.getStPriceTable();
		List<double[]> datas= spt.getDatas();
		String[] rowType=spt.getRowTypes();
		int[] persons_num = null;
		int groupType=routeVo.getGroupType();
		if(groupType==Constants.GROUP_TYPE_SIX){
			persons_num=SixStPrice.PERSON_NUMS;
		}else if(groupType==Constants.GROUP_TYPE_SEVEN){
			persons_num=SevenStPrice.PERSON_NUMS;
		}
		
		for(int i=0;i<rowType.length;i++){//每行	
			double[] rows=datas.get(i);
			for(int j=0;j<rows.length;j++){//每列
				double newCarPrice=Arith.div(newGuide.getPrice(), persons_num[j],Constants.XIAOSHU);
				double oldCarPrice=Arith.div(oldGuide.getPrice(),persons_num[j],Constants.XIAOSHU);
				double diffPrice=Arith.sub(newCarPrice,oldCarPrice);
				rows[j]=Arith.add(rows[j], diffPrice);
				
			}//end for j			
		}//end for i
	}

	public void calcPrice(TravelItemInfoDomain travelItem, Resource resource,
			int personNum) {
		GuideInfoDomain guide=(GuideInfoDomain) resource;
		double price=Arith.div(guide.getPrice(), personNum,Constants.XIAOSHU);//根据人数计算平均价格
		travelItem.setPrice(price);
	}

	public String[] getHeader() {
		return header;
	}

	public ResTable getResDetailTable(String cityId, String resId, int groupType) {
		//判断团队类型
		String[] column=new String[]{};
		int[] persons_num = new int[]{};
		if(groupType==Constants.GROUP_TYPE_SIX){
			column=SixStPrice.COLUMN;
			persons_num=SixStPrice.PERSON_NUMS;
		}else if(groupType==Constants.GROUP_TYPE_SEVEN){
			column=SevenStPrice.COLUMN;
			persons_num=SevenStPrice.PERSON_NUMS;
		}
		
		//获得导游信息
		String[] header=new String[]{"人数","语种","价格"};
		GuideInfoDomain guide=guideInfoDao.getGuideById(resId);
		if(guide==null)
			throw new BusinessException("该导游信息不存在，请联系管理员");
		
		//获得城市的标准配套价格
		List<Map<String, Object>> mapList=cityInfoDao.queryCityStdInfo(new String[]{cityId});
		if(mapList.size()==0)
			throw new BusinessException("设置接送信息失败，该目的城市不存在");
		Map<String, Object> carStMap=mapList.get(0);
		String countryId=carStMap.get("countryId").toString();
		double guidePrice= carStMap.get("guidePrice")==null?0.0d:Double.parseDouble(carStMap.get("carPrice").toString());
		if(groupType==Constants.GROUP_TYPE_SIX)//如果是1-6人团则不去减标准配套价格中的导游价格
			guidePrice=0;
		guidePrice=ExchangeUtil.changeMoney(guidePrice, countryId);//汇率转换
		
		List<Object[]> datas=new ArrayList<Object[]>();
		for(int i=0;i<persons_num.length;i++){
			Object[] obj=new Object[header.length];
			obj[0]=persons_num[i];//人数
			String guideLan="";
			if(guide.getGuideLan()==Constants.GUIDE_LAN_CN)
				guideLan="中文";
			else if(guide.getGuideLan()==Constants.GUIDE_LAN_EN)
				guideLan="英文";
			else
				guideLan="其他";
			obj[1]=guideLan;
			double newguidePrice=ExchangeUtil.changeMoney(guide.getPrice(), countryId);
			double diffPrice=Arith.sub(Arith.div(newguidePrice, persons_num[i],Constants.XIAOSHU),Arith.div(guidePrice,persons_num[i],Constants.XIAOSHU));				
			String strDiff="[+"+Stringer.formatNumber(diffPrice,"0")+"]起";
			if(diffPrice<0){
				strDiff="["+Stringer.formatNumber(diffPrice,"0")+"]起";
			}
			obj[2]=strDiff;
			
			datas.add(obj);
		}
		Page page=new Page();
		page.setRows(datas);
		
		ResTable rt=new ResTable();
		rt.setHeader(header);
		rt.setPage(page);
		rt.addRemark("描述", guide.getRemark());
		rt.setSingleSelect(false);
		return rt;
	}

	public ResTable getResTable(String cityId, int groupType, DataGridModel dgm) {
		//根据团队类型判断,最大人数
		int personNum=6;
		if(groupType==Constants.GROUP_TYPE_SIX)
			personNum=SixStPrice.MAX_PERSON_NUM;
		else if(groupType==Constants.GROUP_TYPE_SEVEN)
			personNum=SevenStPrice.MAX_PERSON_NUM;
		
		//根据城市信息查询车辆信息
		GuideInfoDomain gid=new GuideInfoDomain();
		gid.setCityId(cityId);
		dgm.setSort("price");//设置排序字段
		dgm.setOrder("asc");//设置排序规则
		Page page= guideInfoDao.query(dgm, gid);
		List<GuideInfoDomain> list=page.getRows();
		
		//获得城市的标准配套价格
		List<Map<String, Object>> mapList=cityInfoDao.queryCityStdInfo(new String[]{cityId});
		if(mapList.size()==0)
			throw new BusinessException("设置导游信息失败，该目的城市不存在");
		Map<String, Object> carStMap=mapList.get(0);
		String countryId=carStMap.get("countryId").toString();
		double guidePrice= carStMap.get("guidePrice")==null?0.0d:Double.parseDouble(carStMap.get("guidePrice").toString());
		if(groupType==Constants.GROUP_TYPE_SIX)//如果是1-6人团则不去减标准配套价格中的导游价格
			guidePrice=0;
		guidePrice=ExchangeUtil.changeMoney(guidePrice, countryId);//汇率转换

		//封装表格每行的数据
		List<Map<String, Object>> datas=new ArrayList<Map<String,Object>>();
		for(int i=0;i<list.size();i++){
			GuideInfoDomain guide=list.get(i);
			Map<String, Object> map=new LinkedHashMap<String, Object>();
			map.put(header[0], guide.getGuideId());
			String guideLan="";
			if(guide.getGuideLan()==Constants.GUIDE_LAN_CN)
				guideLan="中文";
			else if(guide.getGuideLan()==Constants.GUIDE_LAN_EN)
				guideLan="英文";
			else
				guideLan="其他";
			map.put(header[1], guideLan);//语种
			map.put(header[2], guide.getBeginTime()+"-"+guide.getEndTime());//时间段
				
			double newGuidePrice=ExchangeUtil.changeMoney(guide.getPrice(), countryId);
			double diffPrice=Arith.sub(Arith.div(newGuidePrice, personNum,Constants.XIAOSHU),Arith.div(guidePrice,personNum,Constants.XIAOSHU));				
			String strDiff="[+"+Stringer.formatNumber(diffPrice,"0")+"]起";
			if(diffPrice<0){
				strDiff="["+Stringer.formatNumber(diffPrice,"0")+"]起";
			}
			map.put(header[3], strDiff);
			map.put("查看详情", "<a href='javascript:void(0);' onclick=\"showDetail('"+cityId+"',"+Constants.RES_TYPE_GUIDE+",'"+guide.getGuideId()+"')\">查看详情</a>");
			datas.add(map);		
		}
		page.setRows(datas);//重新设置分页的数据
		
		ResTable rt=new ResTable();
		rt.setHeader(header);
		rt.setPage(page);
		rt.setSingleSelect(true);
		return rt;
		
	}

	public boolean isSingleSelect() {
		return true;
	}

}
